//递归
function flat1(arr) {
  let res = [];
  for (let i = 0; i < arr.length; i++) {
    if (Array.isArray(arr[i])) {
      res = res.concat(flat1(arr[i]));
    } else {
      res.push(arr[i]);
    }
  }
  return res;
}

//reduce
function flat2(arr) {
  return arr.reduce((res, item) => {
    return res.concat(Array.isArray(item) ? flat2(item) : item);
  }, []);
}

//...扩展运算符
function flat3(arr) {
  while (arr.some((item) => Array.isArray(item))) {
    arr = [].concat(...arr);
  }
  return arr;
}

//split和toString
function flat4(arr) {
  return arr
    .toString()
    .split(",")
    .map((item) => Number(item));
}

//flat
function flat5(arr) {
  return arr.flat(Infinity);
}

//正则和JSON
function flat6(arr) {
  let str = JSON.stringify(arr);
  str = str.replace(/(\[|\])/g, "");
  str = "[" + str + "]";
  return JSON.parse(str);
}

const arr = [1, [2, 3, [4, [5]]]];
const res = flat6(arr);
console.log(res);
